//
//  reFreshView.swift
//  WeiBo test
//
//  Created by Aquarius_wing on 16/5/11.
//  Copyright © 2016年 Wing. All rights reserved.
//

import UIKit

class ReFreshView: UIView ,UIScrollViewDelegate{
    var scrollView:UIScrollView!
    var progress:CGFloat = 0.0
    var shapeLayer = CAShapeLayer()
    //MARK: 初始化
    init(frame: CGRect,scrollView:UIScrollView) {
        super.init(frame: frame)
        self.scrollView=scrollView
        scrollView.delegate=self
        shapeLayer.strokeColor = UIColor.grayColor().CGColor
        layer.addSublayer(shapeLayer)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    convenience init(scrollView:UIScrollView){
        if let sv = scrollView.superview {
            self.init(frame:CGRectMake(0,-80, sv.frame.size.width ,80),scrollView:scrollView)
        }else{
            self.init(frame:CGRectMake(0,-80, scrollView.frame.size.width ,80),scrollView:scrollView)
        }
    }
    //MARK: scrollView的代理
    //定义一个变量用于判断是否在刷新
    var isRefreshing = false
    //下拉结束
    func scrollViewWillEndDragging(scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
        //如果没有在刷新 且 下拉进程完毕
        if !isRefreshing && progress >= 1{
            //开始动画
            beginReFresh()
            //执行刷新任务
            delegate?.doReFresh(self)
        }
    }
    //下拉开始
    func scrollViewDidScroll(scrollView: UIScrollView) {
        //向上滑的距离
        let offY=max(-(scrollView.contentOffset.y+scrollView.contentInset.top),0)
        //scrollView上头的最大高度
        let height:CGFloat=80
        progress=min(offY/height, 1.0)
//        print(progress)
        reDraw(offY)
    }
    //MARK: 绘制
    func reDraw(offY:CGFloat){
//        print("reDraw")
        //如果没在执行动画
        if !isAnimating{
            shapeLayer.lineWidth = 0
            shapeLayer.lineDashPattern = []
            shapeLayer.fillColor=UIColor.grayColor().CGColor
            let y = frame.size.height - offY + 1
            let width = frame.size.width * progress * 0.8 > 15 ? 15:frame.size.width * progress * 0.8
            //画椭圆
            shapeLayer.path = UIBezierPath(ovalInRect: CGRectMake((frame.size.width/2) - 7.5 , y , width , frame.size.height * progress * 0.8)).CGPath
        }

    }
    //结束动画
    func endRefresh(){
//        print("endRefresh")
        isRefreshing = false
        isAnimating = false
        UIView.animateWithDuration(0.3) {
            var inSet = self.scrollView.contentInset;
            inSet.top -= self.frame.size.height
            self.scrollView.contentInset = inSet
        }
    }
    //MARK: 代理
    var delegate:ReFreshViewDelegate?
    //是否在动画
    var isAnimating = false
    //开始动画
    func beginReFresh(){
//        print("beginReFresh")
        //正在刷新
        isRefreshing=true
        //正在动画
        isAnimating=true
        UIView.animateWithDuration(0.3) {
            var inSet = self.scrollView.contentInset;
            inSet.top += self.frame.size.height
            self.scrollView.contentInset = inSet
        }
        //动画
        let keyAnimation = CAKeyframeAnimation(keyPath: "path")
        keyAnimation.duration = 0.8
        keyAnimation.values = [UIBezierPath(ovalInRect: CGRectMake((self.frame.size.width/2) - 20 , self.frame.size.height/2 - 15 , 10 , 10 )).CGPath ,
                               UIBezierPath(ovalInRect: CGRectMake((self.frame.size.width/2) - 8, self.frame.size.height/2 - 2 , 30 , 30 )).CGPath,
                               UIBezierPath(ovalInRect: CGRectMake((self.frame.size.width/2) - 18 , self.frame.size.height/2 - 18 , 20 , 20 )).CGPath,
                               UIBezierPath(ovalInRect: CGRectMake((self.frame.size.width/2) - 12 , self.frame.size.height/2 - 7 , 35 , 35 )).CGPath,
                               UIBezierPath(ovalInRect: CGRectMake((self.frame.size.width/2) - 17 , self.frame.size.height/2 - 17 , 28 , 28 )).CGPath,
                               UIBezierPath(ovalInRect: CGRectMake((self.frame.size.width/2) - 15 , self.frame.size.height/2 - 13 , 33 , 33 )).CGPath,
                               UIBezierPath(ovalInRect: CGRectMake((self.frame.size.width/2) - 15 , self.frame.size.height/2 - 15 , 30 , 30 )).CGPath
        ]
        
        self.shapeLayer.addAnimation(keyAnimation, forKey: nil)
        
        self.shapeLayer.path = UIBezierPath(ovalInRect: CGRectMake((self.frame.size.width/2) - 15 , self.frame.size.height/2 - 15 , 30 , 30 )).CGPath
        
        shapeLayer.fillColor = UIColor.clearColor().CGColor
        shapeLayer.lineWidth = 4
        shapeLayer.lineDashPattern = [2]
        let baseAnimation = CABasicAnimation(keyPath: "strokeEnd")
        baseAnimation.duration = 2
        baseAnimation.fromValue = 0
        baseAnimation.toValue = 1
        baseAnimation.repeatDuration = 5
        shapeLayer.addAnimation(baseAnimation, forKey: nil)
    }
    
    
    /*
    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func drawRect(rect: CGRect) {
        // Drawing code
    }
    */

}
protocol ReFreshViewDelegate {
    func doReFresh(reFreshView:ReFreshView)
}
